package com.aruio.schedule;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @Description aruio 定时任务处理器
 * @Date 2024/9/20 14:31
 * @Created by 10574
 */
@Component
@Slf4j
public class AruioTaskService {
    /**
     * 上一次 开始执行时间点 之后 10 秒再执行
     */
    @Scheduled(fixedRate = 10 * 1000L)
    public void scheduleFixRate() {
        log.info("--------------fix rate--------------");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("--------------fix rate finish--------------");
    }

    /**
     * 上一次 执行完毕时间点 之后 10 秒再执行
     */
    @Scheduled(fixedDelay = 10 * 1000L)
    public void scheduleFixDelay() {
        log.info("--------------fix delay--------------");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("--------------fix delay finish--------------");
    }

    /**
     * 上一次 执行完毕时间点 之后 10 秒再执行
     */
    @Scheduled(initialDelay = 2000L, fixedRate = 10 * 1000L)
    public void scheduleInitialDelayAndFixDelay() {
        log.info("--------------initial delay and fix rate--------------");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("--------------initial delay and fix rate finish--------------");
    }

    /**
     * 根据 cron 表达式定义，每隔 10 秒执行一次
     */
    @Scheduled(cron = "0/10 * * * * *")
    public void scheduleInitialDelayAndWithCron() {
        log.info("--------------cron--------------");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("--------------cron finish--------------");
    }
}
